<template>
  <el-container>
    <el-header>资源管理</el-header>
    <el-main>
      <el-col :span="1"><div class="grid-content bg-purple-light">
        <el-button class="table-header-btn" type="primary" icon="el-icon-plus" size="mini" @click="addClick">新增</el-button>
      </div></el-col>
      <template>
        <el-table :data="sourceList" stripe border style="width: 100%">
          <el-table-column prop="name" label="文件名称" width="180" />
          <el-table-column prop="filepath" label="文件保存路径" />
          <el-table-column prop="url" label="URL" width="180" />
          <el-table-column prop="type" label="类型" width="180" />
          <el-table-column fixed="right" label="操作" width="180">
            <template slot-scope="scope">
              <el-button type="text" size="small" @click="deleteClick(scope.row)">删除</el-button>
            </template>
          </el-table-column>

        </el-table>
      </template>
    </el-main>

    <el-footer>
      <div class="block">
        <el-pagination
          :current-page="page.current"
          :page-sizes="[5,10, 20, 50, 100]"
          :page-size="page.size"
          layout="total, sizes, prev, pager, next, jumper"
          :total="page.total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
        />
      </div>
    </el-footer>

    <el-dialog :title="form.title" :visible.sync="dialogFormVisible" :close-on-click-modal="false" center>
      <el-form :model="tempAttribute" label-width="130px" :inline="true">
        <el-form-item label="资源名称">
          <el-input v-model="tempAttribute.name" />
        </el-form-item>
        <el-form-item label="资源类型">
          <el-select v-model="tempAttribute.source_type" placeholder="请选择">
            <el-option
              v-for="item in dir_source_type"
              :key="item.id"
              :label="item.label"
              :value="item.data"
            />
          </el-select>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="addTask">确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog
      title="删除确认"
      :visible.sync="dialogConfirmVisible"
      :close-on-click-modal="false"
      width="30%"
      center
    >
      <span>{{ deleteMessage }}</span>
      <span slot="footer" class="dialog-footer">
        <el-button @click="dialogConfirmVisible = false">取 消</el-button>
        <el-button type="danger" @click="deleteRoleTask">确 定</el-button>
      </span>
    </el-dialog>

  </el-container>
</template>
<script>
import request from '@/utils/request'
export default {
  data: function() {
    return {
      deleteMessage: '确认删除该条记录吗？',
      tempAttribute: {},
      form: {
        title: '资源添加'
      },
      page: {
        current: 1,
        size: 5,
        total: 0
      },
      sourceList: [],
      currentAtest: {},
      dialogFormVisible: false,
      dialogConfirmVisible: false
    }
  },
  created() {
    this.query()
  },
  methods: {
    // 新增属性任务
    addTask: function() {
      this.$progress.start()
      request({
        url: '/system/role/add',
        method: 'post',
        data: this.tempRole
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.dialogFormVisible = false
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }
      ).catch(() => { this.$progress.done() })
    },
    deleteClick: function(row) {
      this.currentAtest = row
      this.dialogConfirmVisible = true
    },
    deleteTask: function() {
      this.$progress.start()
      request({
        url: '/system/role/delete',
        method: 'post',
        data: this.currentAtest
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.dialogConfirmVisible = false
          this.query()
          this.$message.success(res.msg)
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    },
    // 新增资源点击事件
    addClick: function() {
      this.dialogFormVisible = true
    },
    // 每页显示数目发生变化时
    handleSizeChange: function(val) {
      this.page.size = val
      this.page.current = 1
      this.query()
    },
    // 当前页发生变化时
    handleCurrentChange: function(val) {
      this.page.current = val
      this.query()
    },
    query: function() {
      this.$progress.start()
      request({
        url: '/bengbeng/attribute/query',
        method: 'post',
        data: this.page
      }).then(res => {
        this.$progress.done()
        if (res.result) {
          this.page.total = res.data.total
          this.sourceList = res.data.data
        } else {
          this.$message.error(res.msg)
        }
      }).catch(() => { this.$progress.done() })
    }
  }
}
</script>
